// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import HeroContact, {colliderGroup} from "./HeroContact";
import Music, {musicName} from "../PublicScript/Music";

const {ccclass, property} = cc._decorator;

@ccclass
export default class MirrorGross extends cc.Component {

   @property(cc.Node)
   targetNode: cc.Node = null;

   @property({tooltip: "角度限制"})
   maxAngle = 1;

   initAngle = 0;

    // onLoad () {}

    start () {
        this.initAngle = this.targetNode.angle;
    }

    onBeginContact(contact, self, other) {
        let normal = contact.getWorldManifold().normal;
        if(normal.y != 0){
            contact.disabled = true;
        }
    }

    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve(contact, self, other) {
        if(other.node.group == colliderGroup.hero && other.node.getComponent(HeroContact).move_lock){
            Music.specialMusic({name: musicName.tuigreen, loop: true});
            let normal = contact.getWorldManifold().normal;
            this.nodeAction(-normal.x);
        }
    }

    //做对应的操作
    event(angle){
        this.targetNode.runAction(
            cc.rotateTo(0.1, this.initAngle + angle)
        )
    }

    nodeAction(dir){
        if(Math.abs(this.node.x + (dir/this.maxAngle)) >= 150){
            return;
        }
        this.event((this.node.x / 150) * 50 * this.maxAngle);
        this.node.runAction(
            cc.sequence(
                cc.callFunc(()=>{
                    if(Math.abs(this.node.x + (dir/this.maxAngle)) >= 150){
                        this.node.stopAllActions()
                    }
                }),
                cc.moveBy(0.1, cc.v2(1 * dir,0)),

            )
        )
    }

    // update (dt) {}
}
